package maitaole.service.impl;

import maitaole.bean.Role;

import maitaole.dao.roleDao.RoleDao;
import maitaole.service.IRoleService;
import maitaole.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author 李杰
 * @Description 类的功能描述
 * @Date 2023/5/16
 */
public class RoleServiceImpl implements IRoleService {

    RoleDao roleDao =new RoleDao();

    @Override
    public List<Role> getRoles() {


        List<Role> roles = roleDao.selectRoles();

        return roles;
    }


    @Override
    public List<Role> pageRoles(Integer pageNum, Integer pageSize) {
        List<Role> roles = roleDao.selectRolesPage(pageNum, pageSize);
        return roles;
    }


    @Override
    public boolean addRoleAndMenu(Role role, String[] menuIds) {
        Connection conn = JdbcUtils.begin();
        int i = roleDao.insertRole2(role,conn);
        for (String menuId : menuIds) {
            Integer value = Integer.valueOf(menuId);
            int j = roleDao.insertRoleAndMenu2(i, value,conn);

            if (i==0){
                try {
                    conn.rollback();
                    return false;
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }else {
                try {
                    conn.commit();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        RoleServiceImpl roleService = new RoleServiceImpl();
    }
}

